(function(){
    "use strict"

    function goHome(){
        window.open('http://elewen.oschina.io')
    }

    const line_length = document.querySelectorAll('tr').length

    let cells = document.querySelectorAll('td')

    function cellVal(goWhere) {

        for (let i = 0; i < line_length; i++) {
            // 指定移动方向（水平或竖直）
            let l_r = (goWhere === 'top' || goWhere === 'bottom') ? 'r' : 'l'
            let r = document.querySelectorAll('.' + l_r + i)

            // dom反转（仅向下和向右时需要）
            if (goWhere === 'bottom' || goWhere === 'right') {
                let r_reverse = []
                for (let i = 0; i < r.length; i++) {
                    // console.log(r[r.length - 1 - i])
                    r_reverse.push(r[r.length - 1 - i])
                }
                r = r_reverse
            }

            let r_before = []

            // 提取不为空的cell到r_before中
            for (let j = 0; j < r.length; j++) {
                if (r[j].innerText !== '') {
                    r_before.push(r[j].innerText)
                }
            }

            // console.log(r_before.length)
            let r_after = []

            // 合并或移动不为空的cell到r_after中
            for (let k = 0; k < r_before.length; k++) {
                // console.log(r_before[k], r_before[k + 1])
                /*
                if (r_before[k] == r_before[k + 1]) {
                    r_after.push(r_before[k] * 2)
                    k++
                } else {
                    r_after.push(r_before[k])
                }
                */

                if (
                    (Number(r_before[k]) == '2' && Number(r_before[k + 1]) == '3') ||
                    (Number(r_before[k]) == '3' && Number(r_before[k + 1]) == '2') ||
                    (Number(r_before[k]) > 3 && Number(r_before[k + 1]) > 3 && Number(r_before[k]) == Number(r_before[k + 1]))
                ) {
                    r_after.push(Number(r_before[k]) + Number(r_before[k + 1]))
                    k++
                } else {
                    r_after.push(r_before[k])
                }
            }

            // 将合并和移位后的数字渲染到页面
            for (let q = 0; q < r.length; q++) {
                // console.log(r_after[q])
                r[q].innerText = r_after[q] || ''
            }
        }
    }

    // 产生2
    function create2() {
        let cells_empty = []
        // 找出空着的cell
        cells.forEach(function (val, key) {
            if (cells[key].innerText === '') {
                cells_empty.push({[key]: cells[key].innerText})
            }
        })

        if (cells_empty.length <= 0) {
            console.log('dont have empty cell！')
            var cons_r = confirm('game over! 重新开始吗？')
            if (cons_r == true) {
                location.reload()
            }
            return
        }

        // let ind = Math.ceil(Math.random() * cells_empty.length)
        let ind = Math.floor(Math.random() * cells_empty.length)

        for (let i in cells_empty[ind]) {
            let num = Math.floor(Math.random() * 2) + 2 // 2 or 3
            cells[i].innerText = num

        }
    }

    //    键盘事件监听
    document.onkeydown = function (e) {
        if ([37, 38, 39, 40, 65, 68, 83, 87].indexOf(e.keyCode) >= 0) {

            // 移动与合并
            switch (e.keyCode) {
                case 87:
                case 38:
                    cellVal('top')
                    break
                case 83:
                case 40:
                    cellVal('bottom')
                    break
                case 65:
                case 37:
                    cellVal('left')
                    break
                case 68:
                case 39:
                    cellVal('right')
                    break
            }

            // 产生新2||3
            create2()
        } else {
            // console.log('点击无效')
        }
    }
})()